<extend name="Public:common"/>
<block name="css">
    <link rel="stylesheet" href="__STATIC__/layui/css/layui.css"  media="all">
    <style>
        .newMess th{text-align:center;}
        th,td{padding: 10px;}
    </style>
</block>
<block name="content">
    <div class="page-header"><h1><i class="fa fa-home"></i> 首页>商家管理>酒店供应商>添加房间</h1></div>
    <div class="container-fluid">
        <form action="" method="post">
            <input type="hidden" value="{:I('supplier_id')}" name="supplier_id">
            <table class="table table-striped table-bordered table-hover table-condensed text-center">
                <tbody>
                <tr>
                    <td style="width:25%;"><b> <span class="required">*</span>星级</b></td>
                    <td class="text-left">
                        <select name="star_level" id="stars">
                            <option value="1">一星</option>
                            <option value="2">二星</option>
                            <option value="3">三星</option>
                            <option value="4">四星</option>
                            <option value="5">五星</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td style="width:25%;"><b> <span class="required">*</span>房间类型</b></td>
                    <td class="text-left">
                        <input type="text" name="room_name" placeholder="如:单人房" required>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="width:25%;">
                        <p>
                            <b> <span class="required">*</span>价格信息</b>
                        </p>

                        <table class="table table-striped table-bordered table-hover table-condensed text-center" id="addTr">
                            <tbody>
                            <tr>
                                <td rowspan="2" style="width:15%">币种</td>
                                <td colspan="2" style="width:15%">外币成本</td>
                                <td  style="width:15%">汇率</td>
                                <td colspan="2" style="width:15%">人民币成本</td>
                                <td rowspan="2" style="width:15%">房间数量</td>
                            </tr>
                            <tr>
                                <td>儿童价</td>
                                <td>成人价</td>
                                <td>汇率</td>
                                <td>儿童价</td>
                                <td>成人价</td>
                            </tr>
                            <tr id="tr_">
                                <td>
                                    <input class="form-control"  type="text" name="room[id][money_type]" value="人民币" required>
                                </td>
                                <td>
                                    <input class="form-control related"  type="text" name="room[id][child_foreign_cost]">
                                </td>
                                <td>
                                    <input class="form-control related"  type="text" name="room[id][adult_foreign_cost]">
                                </td>
                                <td>
                                    <input class="form-control exchange_rate"  type="text" name="room[id][exchange_rate]" value="1">
                                </td>
                                <td>
                                    <input class="form-control related"  type="text" name="room[id][child_actual_cost]" required>
                                </td>
                                <td>
                                    <input class="form-control related"  type="text" name="room[id][adult_actual_cost]" required>
                                </td>
                                <td>
                                    <input class="form-control"  type="text" name="room[id][room_count]" required>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <a class="btn btn-info" href="{:U('hotelProductById',array('id'=>I('supplier_id')))}">返回</a>
                        <!-- <a href="{:U('trafficShops')}" class="btn btn-info">返回</a>-->
                        <input type="submit" class="btn btn-primary" value="保存">
                    </td>
                </tr>
                </tbody>
            </table>
        </form>
    </div>
</block>
<block name="js">
    <script>
        //根据汇率自动转换价格
        var foreign=['child_foreign_cost','adult_foreign_cost'];
        var actual=['child_actual_cost','adult_actual_cost'];
        var _foreign,_actual;
        $('body').on('change','.related',function () {
            var name=$(this).attr('name');
            var val=$(this).val();
            var multiply_divide=1;//乘除
            if(name.indexOf('foreign')==-1){
                _foreign=actual;
                _actual=foreign;
                multiply_divide=0;
            }else{
                _foreign=foreign;
                _actual=actual;
                multiply_divide=1;
            }
            for(var i in _foreign){
                //汇率
                var exchangeName=name.replace(_foreign[i],'exchange_rate');
                var exchange_rate=  $('input[name=\''+exchangeName+'\']').val();
                if(!isNaN(exchange_rate)){
                    if(name.indexOf(_foreign[i])!=-1){
                        var changeName=name.replace(_foreign[i],_actual[i]);
                        var result=(val*exchange_rate).toFixed(2);
                        if(multiply_divide){
                            result=(val/exchange_rate).toFixed(2);
                        }
                        $('input[name=\''+changeName+'\']').val(result);
                    }
                }
            }
        });

        //改变汇率更换值
        $('body').on('change','.exchange_rate',function () {
            var reateName=$(this).attr('name');
            var val=$(this).val();
            for(var i in foreign){
                var name=reateName.replace('exchange_rate',foreign[i]);
                var tempVal=$('input[name=\''+name+'\']').val();
                if(tempVal){
                    var actualName=reateName.replace('exchange_rate',actual[i]);
                    var result=(tempVal/val).toFixed(2);
                    $('input[name=\''+actualName+'\']').val(result);
                }else{
                    var name=reateName.replace('exchange_rate',actual[i]);
                    var tempVal=$('input[name=\''+name+'\']').val();
                    if(tempVal){
                        var result=(tempVal*val).toFixed(2);
                        var foreignName=reateName.replace('exchange_rate',foreign[i]);
                        $('input[name=\''+foreignName+'\']').val(result);
                    }
                }
            }
        });

        //限制只能输入2位小数
        function num(obj)
        {
            //先把非数字的都替换掉，除了数字和.
            obj.value = obj.value.replace(/[^\d.]/g,"");
            //必须保证第一个为数字而不是.
            obj.value = obj.value.replace(/^\./g,"");
            //保证只有出现一个.而没有多个.
            obj.value = obj.value.replace(/\.{2,}/g,".");
            //保证.只出现一次，而不能出现两次以上
            obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            obj.value=obj.value.substring(0,5);
        }

    </script>
</block>